package gui;

import java.awt.Desktop;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import logic.ReportsControl;
import common.ChatIF;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ReportsPanel extends JPanel implements ChatIF{
	AcademicFrame frame;
	InformationDepartmentManagerPanel informationDepartmentManagerPanel;
	String month;
	String year;
	String from;
	String to;

	private JComboBox<String> comboBox;
	private JButton activitybtnNewButton;
	private JButton executionbtnNewButton;
	private JButton delaysbtnNewButton;
	private JButton btnNewButton;
	
	public ReportsPanel(AcademicFrame frame, InformationDepartmentManagerPanel informationDepartmentManagerPanel, String month, String year){
		super();
		this.frame=frame;
		this.informationDepartmentManagerPanel=informationDepartmentManagerPanel;
		this.month=month;
		this.year=year;
		frame.setSize(500, 400);
		
		initialize();
	}
	
	private void initialize() {
		setLayout(null);
		
		JLabel lblReports = new JLabel("Reports");
		lblReports.setFont(new Font("Tahoma", Font.BOLD, 26));
		lblReports.setBounds(40, 0, 145, 58);
		add(lblReports);
		
		activitybtnNewButton = new JButton("Activity Report");
		activitybtnNewButton.setEnabled(false);
		activitybtnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				from=comboBox.getSelectedItem().toString();
				to=comboBox.getSelectedItem().toString();
				
				System.out.println(from);
				System.out.println(to);
				
				ArrayList<Object> activity=new ArrayList<Object>();
				activity.add("getActivityReport");
				activity.add(from);
				activity.add(to);
				AcademicFrame.myclient.sendMessageToServer(activity);		
			}
		});
		activitybtnNewButton.setBounds(25, 161, 160, 23);
		add(activitybtnNewButton);
		
		executionbtnNewButton = new JButton("Execution Report");
		executionbtnNewButton.setEnabled(false);
		executionbtnNewButton.setBounds(25, 195, 160, 23);
		add(executionbtnNewButton);
		
		delaysbtnNewButton = new JButton("Delays Report");
		delaysbtnNewButton.setEnabled(false);
		delaysbtnNewButton.setBounds(25, 229, 160, 23);
		add(delaysbtnNewButton);
		
		btnNewButton = new JButton("Cancel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setContentPane(informationDepartmentManagerPanel);
				frame.setSize(500, 700);
			}
		});
		btnNewButton.setBounds(25, 263, 160, 23);
		add(btnNewButton);
		
		comboBox = new JComboBox<String>();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!comboBox.getSelectedItem().toString().equals("--Select date--")){
					activitybtnNewButton.setEnabled(true);
					executionbtnNewButton.setEnabled(true);
					delaysbtnNewButton.setEnabled(true);
				}
				else{
					activitybtnNewButton.setEnabled(false);
					executionbtnNewButton.setEnabled(false);
					delaysbtnNewButton.setEnabled(false);
				}
			}
		});
		comboBox.setBounds(25, 80, 197, 20);
		int i=1;
		comboBox.addItem("--Select date--");
		while(!month.equals("0"+Integer.toString(i))){	
			comboBox.addItem("0"+Integer.toString(i)+"/"+year);
			i++;
		}
		
		add(comboBox);
		
	}
	@SuppressWarnings("unchecked")
	public void display(Object message) {
		ArrayList<Object> replyFromServer = (ArrayList<Object>)message; 	
		System.out.println((String)(replyFromServer.get(0)));
		if( ((String)(replyFromServer.get(0))).equalsIgnoreCase("ActivityReport")){
			System.out.println((String)(replyFromServer.get(1)));
			if (((String)(replyFromServer.get(1))).equalsIgnoreCase("GotData") )
			{
				ReportsControl.handelActivityReport(replyFromServer);
			}
			else if(((String)(replyFromServer.get(1))).equalsIgnoreCase("ReportExists")){
				ReportsControl.openReport(replyFromServer);
			}
			else
			{
				System.out.println("error");
				(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
				frame.setContentPane(informationDepartmentManagerPanel);
				frame.setSize(500, 500);
			}
		}
		if (	((String)(replyFromServer.get(0))).equalsIgnoreCase("viewFile")	)
		{					
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("valid") )
			{
				byte[] bytes= (byte[])(replyFromServer.get(2));
				try
				{
					if (bytes != null) {
						Number fileNumber = Math.random() * 1000;
						String fileName = fileNumber.toString() + ".pdf";
						// write the inputStream to a FileOutputStream
						BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
						bos.write(bytes);	
						bos.flush();
						bos.close();	
						Desktop.getDesktop().open(new File(fileName));
					}
					else
					{
						System.out.println("no file");
						(new Dialog ("There is no file for this request.")).setVisible(true);
					}
				}
				catch (IOException ex) {
					// TODO: handle exception
					System.out.println("error");
					(new Dialog ("There is no file for this request.")).setVisible(true);
				}
			}
			else
				if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("notValid") )
				{
					(new Dialog ("An Error occured.")).setVisible(true);
				}
			

		}
		if (	((String)(replyFromServer.get(0))).equalsIgnoreCase("uploadFile")	)
		{
			 System.out.println("in displayyyyyyyyyyyyyyyyyyyyy");
			 System.out.println((String)(replyFromServer.get(1)));
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("fileUploaded") )
			{
				//(new Dialog ("Your report was added, thank you.")).setVisible(true);
				System.out.println("report was added");

			}
			else
				if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("fileNotUploaded") )
				{
					(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
				}
			

		}
		
	}
}
